<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>hljs</title>
        <link rel="stylesheet" href="../../common/css/highlight.github.min.css" />
        <script src="../../common/js/jquery-3.3.1.min.js"></script>
        <script src="../../common/js/highlight.min.js"></script>
        <style>
            #main {
                display: flex;
                flex-direction: column;
            }
            select {
                height: 40px;
            }
            pre {
                flex: 1 1 60vh;
                overflow-y: auto;
            }
            pre code {
                font-family: 'Sarasa Mono SC', monospace;
                font-size: 1rem;
            }
            textarea {
                height: 30vh;
            }
        </style>
    </head>
    <body>
        <div id="main">
            <select id="languages"></select>
            <pre id="code"></pre>
            <textarea id="text"></textarea>
        </div>
    </body>
    <script type="module">
        import { cachedFetch } from '../../common/js/utils/index.js';

        let languagesDom = document.querySelector('#languages');
        let textDom = document.querySelector('#text');
        languagesDom.addEventListener('change', updateText);
        textDom.addEventListener('keyup', updateText);

        cachedFetch('./languages.json', -1)
            .then((response) => response.text())
            .then((text) => {
                let languages = JSON.parse(text);
                for (let key in languages) {
                    let option = document.createElement('option');
                    option.innerText = languages[key];
                    option.value = key;
                    languagesDom.appendChild(option);
                }
                init();
            });

        function updateText(force) {
            try {
                if (force) {
                    let lang = languagesDom.value.split(', ')[0];
                    code.innerHTML = hljs.highlight(text.value, { language: lang, ignoreIllegals: true }).value;
                } else {
                    let highlightAuto = hljs.highlightAuto(text.value);
                    code.innerHTML = highlightAuto.value;
                    if (!highlightAuto.language) {
                        highlightAuto.language = 'text';
                    }
                    $('#languages option').each((index, option) => {
                        option.value.split(', ').forEach((language) => {
                            if (highlightAuto.language === language) {
                                option.defaultSelected = true;
                            }
                        });
                    });
                }
            } catch (e) {
                code.innerText = e;
            }
        }

        function init() {
            textDom.value =
                'function updateText(force) {\n' +
                '    try {\n' +
                '        if (force) {\n' +
                "            let lang = languagesDom.value.split(', ')[0];\n" +
                '            code.innerHTML = hljs.highlight(text.value, { language: lang, ignoreIllegals: true }).value;\n' +
                '        } else {\n' +
                '            let highlightAuto = hljs.highlightAuto(text.value);\n' +
                '            code.innerHTML = highlightAuto.value;\n' +
                '            if (!highlightAuto.language) {\n' +
                "                highlightAuto.language = 'text';\n" +
                '            }\n' +
                "            $('#languages option').each((index, option) => {\n" +
                "                option.value.split(', ').forEach((language) => {\n" +
                '                    if (highlightAuto.language === language) {\n' +
                '                        option.defaultSelected = true;\n' +
                '                    }\n' +
                '                });\n' +
                '            });\n' +
                '        }\n' +
                '    } catch (e) {\n' +
                '        code.innerText = e;\n' +
                '    }\n' +
                '}';
            updateText();
        }
    </script>
</html>
